<?php
namespace Admin\Controller;
use Think\Controller;
use think\Page;
class ReferenceController extends Controller
{
    public function index(){
         
    }
    
    /*
     * 得到一级分类下面的所有子分类
     */
    public function reference_cata_list(){
        if(IS_GET){
            $type=I('get.level_one',1);
            $result=D('Reference')->getCata($type);
            $CataOne=$this->getCataOne();
            $this->assign('CataOne',$CataOne);
            $this->assign('level_one_hid',$type);
            $this->assign('cata_list',$result['list']);
            $this->assign('page',$result['show']);
            $this->display('Reference/Reference_Cata_list');
        }
        
    }
    
    /*
     * 得到分类选择中的一级分类。
     */
    
    public function getCataOne(){
        $result=M('category')->where('level=1')->select();
        if($result){
            return $result;
        }
    }
    
    public function reference_list(){
        if(IS_GET){
               $type=I('get.cat_two');
               if($type!=''){
               $result=D('Reference')->reference_list($type);
               $this->assign('type',$type);
               $this->assign('reference_List',$result['list']);
               $this->assign('page',$result['show']);
               $this->display('Reference/Reference_list');
                     }
        }
        
    }
    
    public function edit_reference(){
        if(IS_GET){
            $re=I('get.');
            if(!empty($re)){
                $type=M('referencetype')->where(['cate_id'=>$re['type']])->select();
                $result=D('Reference')->edit_reference($re['id']);
                $this->assign('cata_two',$re['type']);
                $this->assign('type',$type);
                $this->assign('edit_reference',$result);
                $this->display('Reference/Edit_reference');
            }
        }
        
    }
    /*
     * 进入添加资料页面
     */
    public function add_pa(){
        $level_one=I('get.level_one_hid');
        $level_two=I('get.type');
        $this->assign('level_one',$level_one);
        $this->assign('level_two',$level_two);
        $type=M('referencetype')->where(['cate_id'=>$level_two])->select();
        $this->assign('type',$type);
        $this->display('Reference/add');
    }
    
    /*
     * 设置该二级分类下面的资料价格
     */
    
    public function edit_price(){
        if(IS_POST){
            $data=I('post.');
            $data['cate_id']=$data['type'];
            $ca=M('price')->where(['cate_id'=>$data['cate_id']])->count();
            if($ca==0){
                $value['reference_price']=$data['reference_price'];
                $value['cate_id']=$data['cate_id'];
                $value['exam_price']=0;
                $re=M('price')->add($value);
                $re?$this->success('价格设置成功',U('reference_list',['cat_two'=>$data['cate_id']])):$this->error('价格设置失败');
            }else{
                $value['reference_price']=$data['reference_price'];
                $re=M('price')->where(['cate_id'=>$data['cate_id']])->save($value);
                $re?$this->success('价格设置成功',U('reference_list',['cat_two'=>$data['cate_id']])):$this->error('价格设置失败');
            }
        }else{
            $data=I('get.');
            $price=M('price')->where(['cate_id'=>$data['type']])->field('reference_price')->find();
            $this->assign('price',$price['reference_price']);
            $this->assign('type',$data['type']);
            $this->assign('cata_two',$data['type']);
            $this->display('Reference/edit_reference_price');
        }
    }
    
    public function add(){
        if(IS_POST){
               $re=I('post.');
               $re['addtime']=time();
               $re['type']=($re['type']==null?'':$re['type']);
               $data=D('Reference')->create($re);
            if($data){
               D('Reference')->add()?$this->success('添加资料成功',U('reference_list',['cat_two'=>$re['cata_two']])): $this->error('添加资料失败');
            }
            else $this->error(D('Reference')->getError());
        }
    }
    
    public function save(){
        if(IS_POST){
                $re=I('post.');
                $data=D('Reference')->create($re);
            if($data){
                $map['id']=$data['id'];
                $result=D('Reference')->where($map)->save();
                $result>=0?$this->ajaxReturn('修改成功'):$this->ajaxReturn('修改失败');
            }
            else{
                $this->ajaxReturn(D('Reference')->getError());
            }
        }
                
    }
    
    public function delete_single(){
         if(IS_GET){
             $id=I('get.id');
             if($id!=''){
                 $map['id']=$id;
                 $result=M('Reference')->where($map)->delete();
                 $result>0?$this->success('删除成功'):$this->success('删除失败');
             }
         }
    }
    
    public function delete_all(){
        if(IS_GET){
        $info=I('get.');
        if(!empty($info)){
            $re=explode('@',$info['ids']);
            if(!empty($re)){
                $map['id']=array('in',$re);
                $result=M('Reference')->where($map)->delete();
                $result=$result?true:false;
                $this->ajaxReturn($result);
            }
            else{
              $this->ajaxReturn(false);
            }
        }else{
            $this->ajaxReturn('提交信息出错');
        }
      }
    }
    public function referenceType_list(){
                   $type=I('get.type');
                   $map['cate_id']=$type;
                   $re=M('referencetype')->where($map)->select();
                   $this->assign('cate_type',$type);
                   $this->assign('reType_list',$re);
                   $this->display('Reference/reType_list');
    }
    public function edit_reType(){
                    if(IS_GET){
                        $re=I('get.');
                        if(!empty($re)){
                            $map['id']=$re['id'];
                            $result=M('referencetype')->where($map)->select();
                            $this->assign('cate_id',$re['cate_id']);
                            $this->assign('edit_reType',$result);
                            $this->display('Reference/Edit_reType');
                        }
                    }
    }
    
    public function add_reType(){
                    $cate_id=I('get.cate_id');    
                    $this->assign('cate_id',$cate_id);
                    $this->display('Reference/add_reType');
    }
    
    public function save_reType(){
                      if(IS_GET){
                            $re=I('get.');
                              if($re['id']==''){
                                  $data['type_name']=$re['type_name'];
                                  $data['cate_id']=$re['cate_id'];
                                  if($data['type_name']===''){
                                      $this->error('类型名不能为空');
                                  }
                                      $result=M('referencetype')->add($data);
                                      $result>=0?$this->success('添加成功',U('referenceType_list',['type'=>$re['cate_id']])):$this->error('添加失败');
                                }
                                else{
                                    $map['id']=$re['id'];
                                    $data['type_name']=$re['type_name'];
                                    $result=M('referencetype')->where($map)->save($data);
                                    $result>=0?$this->success('修改成功'):$this->error('编辑失败');
                                }
                        }
                    }
                    
    public function delete_reType(){
        if(IS_GET){
            $re=I('get.id');
            $map['id']=$re;
            $result=M('referencetype')->where($map)->delete();
            $result>=0?$this->success('删除成功'):$this->error('删除失败');
             }
        
          }
  
}
    
   
